home *** CD-ROM | disk | FTP | other *** search
- Path: mudskipper.cac.psu.edu!user
- From: fcusack@tdx.org (frank.)
- Newsgroups: comp.lang.c++
- Subject: Re: beginner question - typecasting
- Date: Sat, 06 Jan 1996 13:57:16 -0500
- Organization: Penn State University, Center for Academic Computing
- Message-ID: <fcusack-0601961357160001@mudskipper.cac.psu.edu>
- References: <4cei1r$s02@sun.cis.smu.edu> <30EBCED7.774@sto.fdata.se> <30EDAE8C.582@wildfire.com>
- NNTP-Posting-Host: mudskipper.cac.psu.edu
-
- In article <30EDAE8C.582@wildfire.com>, Stonewall Ballard
- <stoney@wildfire.com> wrote:
-
- > Niklas Mellin wrote:
- > >
- > > Damon Bowman wrote:
- > > >
- > > > When you are typecasting, is there any difference between:
- > > >
- > > > a = int(x)
- > > > and
- > > > a = (int) x
- > >
- > > [...]
- > >
- > > Yes the first is not type casting, but a "type conversion".
- > >
- > > When x is a variables of built in types there is no
- > > difference. But if x is a varible of some class for instance
- > > the first form generates a call to that class' operator int(),
- > > generating a compile time error message if there is no such
- > > member function.
- > >
- > > The 2nd form is an inheritance from plain C, and doesn't work
- > > when x is of class type. It is also
- > > not "safe", meaning that the compiler will not generate error
- > > messages in certain situations, and you will get buggy code
- > > or run time errors instead.
- >
- > This is not true. The only difference between these forms is the syntax.
- > See section 3.2.5 in Stroustrup's "The C++ Programming Language" 2nd
- > edition.
- >
- > Try compiling this:
- >
- [...]
- >
- > >
- > > In general u should choose the conversion form if possible,
- > > but sometimes a type cast can be handy. One case is if you
- > > are using a 3rd part class library that is not well written
- > > (read MFC) and they forgot to declare function parameters as
- > > const and you have a const object you would like to pass to
- > > that function. Then you can type cast your object or variable
- > > to non const.
-
- Let's get some terms straight. The two types of conversions are
- _equivalent_, as stated. the form int(x) is called "functional notation"
- and the form (int) x is called "cast notation". They are both "type
- conversion".
-
- The only difference is that if you are converting to a type that is not a
- simple type name, you must use cast notation. eg. you cannot use int*(x),
- but must use (int *) x. You can typedef int* to another type name however,
- to use the functional notation.
-
- Again, this is all in Stroustrup 2nd edition, and it's probably in the FAQ.
- ~Frank
-